//
//  ReleaseCommodityViewController.m
//  哈哈哈哈
//
//  Created by CccDaxIN on 2016/10/8.
//  Copyright © 2016年 TYWang. All rights reserved.
//

#import "ReleaseCommodityViewController.h"
#import "ReleaseCommodityModel.h"
#import "shopAddImageView.h"
#import "HomePageViewController.h"

@interface ReleaseCommodityViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
{
    NSInteger recordCount;
    NSInteger clickCount;
    UIView *bView;
    NSString *clsaaNil;

}
@property (nonatomic,strong)UITableView *classTableView;
@property (nonatomic,strong)NSMutableArray *imageArray;
@property (nonatomic,strong)NSMutableArray *image_Array;
@property (nonatomic,strong)NSMutableArray *dataArray;
@end

@implementation ReleaseCommodityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"发布商品";
    [self createReleaseCommoditySubViewORAdjustment];
    [NNSingleton sharedSingleton].setUpORcommodity = @"发布商品";
    /*
     *返回
     */
    UIImage *releaseCommodityBackImage = [UIImage imageNamed:@"左箭头"];
    releaseCommodityBackImage = [releaseCommodityBackImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:releaseCommodityBackImage style:UIBarButtonItemStylePlain target:self action:@selector(releaseCommodityBackImageBackAction)];
    // Do any additional setup after loading the view from its nib.
}
- (void)releaseCommodityBackImageBackAction{
    if ([[NNSingleton sharedSingleton].pop_String isEqualToString:@"扫码"]) {
        
    for (UIViewController *controller in self.navigationController.viewControllers) {
         if ([controller isKindOfClass:[HomePageViewController class]]) {
         [self.navigationController popToViewController:controller animated:YES];
         }
    }
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}
#pragma  mark -- 设置
- (void)createReleaseCommoditySubViewORAdjustment{
    
    /*
     *设置圆角
     */
    [_selectClassButton.layer setMasksToBounds:YES];
    _selectClassButton.layer.cornerRadius = 3;
    
    /*
     button设置
     */
    recordCount = 0;
    clickCount = 0;
    clsaaNil= @"";
    _imageButtonOne.hidden = YES;
    _imageButtonTwo.hidden = YES;
    _imageButtonThree.hidden = YES;
    _deleteButtonOne.hidden = YES;
    _deleteButtonTwo.hidden = YES;
    _deleteButtonThree.hidden = YES;
    _imageArray = [[NSMutableArray alloc]init];
    _image_Array = [[NSMutableArray alloc]init];
    
    _commodityNameTextF.delegate = self;
    _commodityPriceTextF.delegate = self;
//    _promotionPriceTextF.delegate = self;
    _measurementUnitTextF.delegate = self;
    _stockTextF.delegate = self;
    _describeTxxtF.delegate  =self;
    
    /*
     设置scrollview
     */
    self.imageScrollview.showsVerticalScrollIndicator = FALSE;
    self.imageScrollview.contentSize = CGSizeMake(kScrernWidth*2+10,0);
    self.imageScrollview.delegate = self;
    
    if ([[NNSingleton sharedSingleton].releasePosition isEqualToString:@"商品库"]) {
        [self fromCommodityWarehouseGetData];
    }else if ([[NNSingleton sharedSingleton].releasePosition isEqualToString:@"自定义添加"]){
        //展示店铺实景图(最多5张)
        shopAddImageView *shopImageView = [[shopAddImageView alloc]initWithFrame:CGRectMake(0, 5, kScrernWidth*2, 100)];
        [self.imageScrollview addSubview:shopImageView];
    }else if([[NNSingleton sharedSingleton].releasePosition isEqualToString:@"扫码"]){
        //展示店铺实景图(最多5张)
        shopAddImageView *shopImageView = [[shopAddImageView alloc]initWithFrame:CGRectMake(0, 5, kScrernWidth*2, 100)];
        [self.imageScrollview addSubview:shopImageView];
        ReleaseCommoditySubModel *model = [ReleaseCommoditySubModel mj_objectWithKeyValues:[NNSingleton sharedSingleton].scanCode];
        
        self.commodityNameTextF.text = model.ProductName;
        clsaaNil = model.categoryNames;
        [_selectClassButton setTitle:model.categoryNames forState:UIControlStateNormal];
        NSString *price_string = [NSString stringWithFormat:@"%.2f",model.Price];
        self.commodityPriceTextF.text = price_string;
        self.measurementUnitTextF.text = model.MeasureUnit;
        self.describeTxxtF.text = model.Description;
        clickCount++;
    }
}
#pragma  mark -- 从商品库获取的数据
- (void)fromCommodityWarehouseGetData{
    [SVProgressHUD showWithStatus:@"正在加载"];
    [GDJHttpTool get:CommodityDetail params:@{@"ShouJiHao":GETPHONE,@"Duuid":GETUUID,@"Token":GETTOKEN,@"Id":[NNSingleton sharedSingleton].commodityLibraryID} success:^(id responseObj) {
        NSLog(@"无言 %@",responseObj);
        //查看错误码
        NSInteger err = [[responseObj valueForKey:@"Err"]integerValue];
        NSLog(@"争取吧%ld",(long) err);
        if (err == 0) {
            [SVProgressHUD dismiss];
            NSDictionary *dataDic = [responseObj objectForKey:@"Products"];
            ReleaseCommoditySubModel *model = [ReleaseCommoditySubModel mj_objectWithKeyValues:dataDic];
            NSLog(@"你是男的我也爱  --%@",model.ProductName);
            self.commodityNameTextF.text = model.ProductName;
            clsaaNil = model.categoryNames;
            [_selectClassButton setTitle:model.categoryNames forState:UIControlStateNormal];
            
            NSString *price_string = [NSString stringWithFormat:@"%.2f",model.Price];
            if ([price_string isEqualToString:@"0.00"]) {
                self.commodityPriceTextF.text = @"";
            }else{
                self.commodityPriceTextF.text = price_string;
            }
            self.measurementUnitTextF.text = model.MeasureUnit;
            self.describeTxxtF.text = model.Description;
            [NNSingleton sharedSingleton].commodityImage = responseObj[@"Products"];
            //展示店铺实景图(最多5张)
            shopAddImageView *shopImageView = [[shopAddImageView alloc]initWithFrame:CGRectMake(0, 5, kScrernWidth*2, 100)];
            [self.imageScrollview addSubview:shopImageView];
            //            recordCount++;
            clickCount++;
        }else{
            NSLog(@"%@",responseObj[@"ErrMsg"]);
            [SVProgressHUD showErrorWithStatus:responseObj[@"ErrMsg"]];
            return;
        }
    }failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    clsaaNil = @"";
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification {
    /*
     Reduce the size of the text view so that it's not obscured by the keyboard.
     Animate the resize so that it's in sync with the appearance of the keyboard.
     */
    NSDictionary *userInfo = [notification userInfo];
    // Get the origin of the keyboard when it's displayed.
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    // Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.
    CGRect keyboardRect = [aValue CGRectValue];
    keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
    
    CGFloat keyboardTop = keyboardRect.origin.y;
    CGRect tableViewFrame = self.view.bounds;
    tableViewFrame.size.height = keyboardTop - self.view.bounds.origin.y;
    
    // Get the duration of the animation.
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];
    
    // Animate the resize of the text view's frame in sync with the keyboard's appearance.
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:animationDuration];
    
    _scrollView.frame = CGRectMake(0,0, kScrernWidth,keyboardTop);
    [UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)notification {
    
    NSDictionary* userInfo = [notification userInfo];
    /*
     Restore the size of the text view (fill self's view).
     Animate the resize so that it's in sync with the disappearance of the keyboard.
     */
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:animationDuration];
    _scrollView.frame = CGRectMake(0,0, kScrernWidth,kScrernHeight);
    [UIView commitAnimations];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}
//#pragma mark -- 上传图片
//- (IBAction)uploadPictures:(UIButton *)sender {
//
//    switch (sender.tag) {
//        case 1897:{
//            NSLog(@"一图");
//        }
//            break;
//        case 1898:{
//            NSLog(@"二图");
//            [self selectPictureUploadMode];
//        }
//            break;
//        case 1899:{
//            NSLog(@"三图");
//            [self selectPictureUploadMode];
//        }
//            break;
//        default:
//            break;
//    }
//}
//
//#pragma mark -- 上传照片
//- (IBAction)addUploadPictures:(id)sender {
//    NSLog(@"上传图片");
//    [self selectPictureUploadMode];
//}
//#pragma mark -- 删除图片
//- (IBAction)deleteImageButton:(UIButton *)sender {
//
//    switch (sender.tag) {
//        case 2897:{
//            NSLog(@"删一");
//            if (recordCount == 1) {
//                recordCount--;
//                [_imageArray removeAllObjects];
//                [_image_Array removeAllObjects];
//                self.imageButtonOne.hidden = YES;
//                self.imageButtonTwo.hidden = YES;
//                _addCommodityImage.hidden = NO;
//                _deleteButtonOne.hidden = YES;
//                NSLog(@"想念如果会有声音 %lu",(unsigned long)_imageArray.count);
//            }else if (recordCount == 2) {
//                recordCount--;
//                [_imageArray removeObjectAtIndex:0];
//                [_image_Array removeObjectAtIndex:0];
//                NSData *data = [_imageArray objectAtIndex:0];
//                [self.imageButtonOne setBackgroundImage:[UIImage imageWithData:data] forState:UIControlStateNormal];
//                [self.imageButtonTwo setBackgroundImage:[UIImage imageNamed:@"添加照片-"] forState:UIControlStateNormal];
//                self.imageButtonThree.hidden = YES;
//                _addCommodityImage.hidden = YES;
//                _deleteButtonTwo.hidden = YES;
//                NSLog(@"突然好想你 %lu",(unsigned long)_imageArray.count);
//            }else if (recordCount == 3) {
//                recordCount--;
//                [_imageArray removeObjectAtIndex:0];
//                [_image_Array removeObjectAtIndex:0];
//
//                NSData *dataOne = [_imageArray objectAtIndex:0];
//                [self.imageButtonOne setBackgroundImage:[UIImage imageWithData:dataOne] forState:UIControlStateNormal];
//                NSData *dataTwo = [_imageArray objectAtIndex:1];
//                [self.imageButtonTwo setBackgroundImage:[UIImage imageWithData:dataTwo] forState:UIControlStateNormal];
//                [self.imageButtonThree setBackgroundImage:[UIImage imageNamed:@"添加照片-"] forState:UIControlStateNormal];
//                _addCommodityImage.hidden = YES;
//                _deleteButtonThree.hidden = YES;
//                NSLog(@"突然好想你 %lu",(unsigned long)_imageArray.count);
//            }
//        }
//            break;
//        case 2898:{
//            NSLog(@"删二");
//
//            if (recordCount == 2) {
//                recordCount--;
//                [_imageArray removeObjectAtIndex:1];
//                [_image_Array removeObjectAtIndex:1];
//                [self.imageButtonTwo setBackgroundImage:[UIImage imageNamed:@"添加照片-"] forState:UIControlStateNormal];
//                self.imageButtonThree.hidden = YES;
//                self.deleteButtonTwo.hidden = YES;
//                self.addCommodityImage.hidden = YES;
//                NSLog(@"想念如果会有声音 %lu",(unsigned long)_imageArray.count);
//            }else if (recordCount == 3){
//                recordCount--;
//                [_imageArray removeObjectAtIndex:1];
//                [_image_Array removeObjectAtIndex:1];
//                NSData *data = [_imageArray objectAtIndex:1];
//                [self.imageButtonTwo setBackgroundImage:[UIImage imageWithData:data] forState:UIControlStateNormal];
//                [self.imageButtonThree setBackgroundImage:[UIImage imageNamed:@"添加照片"] forState:UIControlStateNormal];
//                self.deleteButtonThree.hidden = YES;
//                _addCommodityImage.hidden = YES;
//                NSLog(@"突然好想你 %lu",(unsigned long)_imageArray.count);
//            }
//        }
//            break;
//        case 2899:{
//            NSLog(@"删三");
//
//            if (recordCount == 3) {
//                recordCount--;
//                [_imageArray removeObjectAtIndex:2];
//                [_image_Array removeObjectAtIndex:2];
//
//                [self.imageButtonThree setBackgroundImage:[UIImage imageNamed:@"添加照片-"] forState:UIControlStateNormal];
//                self.deleteButtonThree.hidden = YES;
//                self.addCommodityImage.hidden = YES;
//            }
//        }
//            break;
//        default:
//            break;
//    }
//}


#pragma mark -- 选择分类
- (IBAction)selectionClassification:(UIButton *)sender {
    NSLog(@"选择分类");
    sender.selected = !sender.selected;
    if (sender.selected) {
        /*取消textfield响应者*/
        [_commodityNameTextF resignFirstResponder];
        [_commodityPriceTextF resignFirstResponder];
//        [_promotionPriceTextF resignFirstResponder];
        [_measurementUnitTextF resignFirstResponder];
        [_stockTextF resignFirstResponder];
        [_describeTxxtF resignFirstResponder];
        
        clickCount = 0;
        [self commodityClassificationDataRequest];
        bView = [[UIView alloc]initWithFrame:CGRectMake(0, kScrernHeight-220, kScrernWidth, 220)];
        bView.backgroundColor = [UIColor colorWithHue:0.21 saturation:1.00 brightness:0.69 alpha:1.00];
        [self.view addSubview:bView];
        /*创建tableview*/
        _classTableView =[[UITableView alloc]initWithFrame:CGRectMake(0,20, kScrernWidth,200) style:UITableViewStylePlain];
        _classTableView.showsVerticalScrollIndicator = FALSE;
        _classTableView.delegate=self;
        _classTableView.dataSource=self;
        _classTableView.tableFooterView = [[UIView alloc] init];
        [bView addSubview:_classTableView];
        
        [self commodityClassificationDataRequest];
    }else{
        bView.hidden = YES;
        [_selectClassButton setTitle:@"选择" forState:UIControlStateNormal];
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifer=@"Mycell";
    UITableViewCell *MyCell=[tableView cellForRowAtIndexPath:indexPath];
    if (MyCell==nil) {
        MyCell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    }
    MyCell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_dataArray.count != 0) {
        ReleaseCommodityModel *model= [_dataArray objectAtIndex:indexPath.row];
        MyCell.textLabel.text = model.Name;
    }
    return MyCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    clickCount++;
    ReleaseCommodityModel *modelSub= [_dataArray objectAtIndex:indexPath.row];
    [NNSingleton sharedSingleton].subId = modelSub.Id;
    [self commodityClassificationDataSubRequest];
    clsaaNil = modelSub.Name;
    if (clickCount == 2) {
        _selectClassButton.selected = NO;
        bView.hidden = YES;
        clsaaNil = modelSub.Name;
        [_selectClassButton setTitle:modelSub.Name forState:UIControlStateNormal];
    }
}
#pragma mark -- 二级分类管理子类数据请求
- (void)commodityClassificationDataSubRequest{
    [_dataArray removeAllObjects];
    
    NSString *subID_str = [NSString stringWithFormat:@"%d",[NNSingleton sharedSingleton].subId ];
    [GDJHttpTool get:TwolevelClassificationSub params:@{@"ShouJiHao":GETPHONE,@"Duuid":GETUUID,@"Token":GETTOKEN,@"ParentId":subID_str} success:^(id responseObj) {
        NSLog(@"树读 %@",responseObj);
        
        //查看错误码
        NSInteger err = [[responseObj valueForKey:@"Err"]integerValue];
        NSLog(@"争取吧%ld",(long) err);
        if (err == 0) {
        _dataArray =   [ReleaseCommodityModel mj_objectArrayWithKeyValuesArray:[responseObj objectForKey:@"SecondCategory"]];
            if (_dataArray.count == 0) {
                clickCount--;
                _selectClassButton.selected = NO;
                bView.hidden = YES;
                [_selectClassButton setTitle:clsaaNil forState:UIControlStateNormal];
            }
        }else{
            NSLog(@"%@",responseObj[@"ErrMsg"]);
            [SVProgressHUD showErrorWithStatus:responseObj[@"ErrMsg"]];
            return;
        }
        [_classTableView reloadData];
    }failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}
#pragma mark -- 一级分类管理父类数据请求
- (void)commodityClassificationDataRequest{
    [GDJHttpTool get:TwolevelClassification params:@{@"ShouJiHao":GETPHONE,@"Duuid":GETUUID,@"Token":GETTOKEN} success:^(id responseObj) {
        NSLog(@"黑暗骑士 %@",responseObj[@"Category"]);
        
        //查看错误码
        NSInteger err = [[responseObj valueForKey:@"Err"]integerValue];
        NSLog(@"争取吧%ld",(long) err);
        if (err == 0) {
            _dataArray =   [ReleaseCommodityModel mj_objectArrayWithKeyValuesArray:[responseObj objectForKey:@"Category"]];
        }else{
            NSLog(@"%@",responseObj[@"ErrMsg"]);
            [SVProgressHUD showErrorWithStatus:responseObj[@"ErrMsg"]];
            return;
        }
        [_classTableView reloadData];
    }failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}
#pragma mark -- 立刻发布
- (IBAction)immediatelyRelease:(id)sender {
    NSLog(@"立刻发布");
    
    if ([[NNSingleton sharedSingleton].imageArray isEqualToString:@"0"]) {
        [SVProgressHUD showErrorWithStatus:@"请上传商品图片" ];
        return;
    }
    if (!self.commodityNameTextF.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入商品名称" ];
        return;
    }
    if ([clsaaNil isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"请选择商品分类" ];
        return;
    }
    if (!self.commodityPriceTextF.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入商品价格" ];
        return;
    }
//    if (!self.promotionPriceTextF.text.length) {
//        [SVProgressHUD showErrorWithStatus:@"请输入促销价" ];
//        return;
//    }
    if (!self.measurementUnitTextF.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入计量单位" ];
        return;
    }
    if (!self.stockTextF.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入库存" ];
        return;
    }
//    if (!self.describeTxxtF.text.length) {
//        [SVProgressHUD showErrorWithStatus:@"请输入商品描述" ];
//        return;
//    }
    [self uploadProductInformation];
}
#pragma mark -- 上传商品信息
- (void)uploadProductInformation{
    [SVProgressHUD showWithStatus:@"正在上传"];
    NSString *string_Id= [NSString stringWithFormat:@"%d",[NNSingleton sharedSingleton].subId];
    [GDJHttpTool post:ReleaseCommodity params:@{@"ShouJiHao":GETPHONE,@"Duuid":GETUUID,@"Token":GETTOKEN,@"Sku":self.stockTextF.text,@"CategoryId":string_Id,@"ProductName":self.commodityNameTextF.text,@"MarketPrice":self.commodityPriceTextF.text,@"MeasureUnit":self.measurementUnitTextF.text,@"ShortDescription":self.describeTxxtF.text,@"ProductPhoto":[NNSingleton sharedSingleton].imageArray} success:^(id responseObj) {
        //查看错误码
        NSInteger err = [[responseObj valueForKey:@"Err"]integerValue];
        NSLog(@"问题 %ld",(long)err);
        if (err == 0) {
            [SVProgressHUD showSuccessWithStatus:@"发布成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }else if(err == 999){
            [SVProgressHUD showErrorWithStatus:responseObj[@"ErrMsg"]];
            return;
        }else{
            NSLog(@"%@",responseObj[@"ErrMsg"]);
            [SVProgressHUD showErrorWithStatus:responseObj[@"ErrMsg"]];
            return;
        }
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}
#pragma mark -- 选择图片上传方式
- (void)selectPictureUploadMode{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选择照片" message:@"从相册选取照片或拍照发送" preferredStyle:UIAlertControllerStyleActionSheet];
    [alert addAction:[UIAlertAction actionWithTitle:@"相册选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //        recordCount++;
        UIImagePickerController *imagePickController = [[UIImagePickerController alloc]init];
        imagePickController.delegate = self;
        imagePickController.allowsEditing = YES;
        imagePickController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:imagePickController animated:YES completion:^{
        }];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            UIImagePickerController *imagePickController = [[UIImagePickerController alloc]init];
            imagePickController.delegate = self;
            imagePickController.allowsEditing = YES;
            imagePickController.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:imagePickController animated:YES completion:^{
            }];
        }]];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [alert dismissViewControllerAnimated:YES completion:nil];
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark - UIImagePickerController Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    [picker dismissViewControllerAnimated:YES completion:nil];
    [self verifyIdentifier:image];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark -- 上传店铺图片
-(void)verifyIdentifier:(UIImage *)image {
    
    [SVProgressHUD showWithStatus:@"正在上传图片"];
    NSData *dataPostOne = [self resetSizeOfImageData:image maxSize:1024];
    NSString * strOne = [dataPostOne base64EncodedStringWithOptions:kNilOptions];
    NSLog(@" 不要不快乐  %lu",(unsigned long)_imageArray.count);
    
    if (recordCount == 1) {
        [SVProgressHUD dismiss];
        self.imageButtonOne.hidden = NO;
        [self.imageButtonOne setBackgroundImage:[UIImage imageWithData:dataPostOne] forState:UIControlStateNormal];
        self.deleteButtonOne.hidden = NO;
        self.imageButtonTwo.hidden = NO;
        _addCommodityImage.hidden = YES;
        [self.imageButtonTwo setBackgroundImage:[UIImage imageNamed:@"添加照片-"] forState:UIControlStateNormal];
        [_imageArray addObject:dataPostOne];
        [_image_Array addObject:strOne];
        
    }else if(recordCount == 2){
        [SVProgressHUD dismiss];
        
        [self.imageButtonTwo setBackgroundImage:[UIImage imageWithData:dataPostOne] forState:UIControlStateNormal];
        self.imageButtonThree.hidden = NO;
        self.deleteButtonTwo.hidden = NO;
        [self.imageButtonThree setBackgroundImage:[UIImage imageNamed:@"添加照片-"] forState:UIControlStateNormal];
        [_imageArray addObject:dataPostOne];
        [_image_Array addObject:strOne];
        
        
    }else if(recordCount == 3){
        [SVProgressHUD dismiss];
        self.imageButtonThree.hidden = NO;
        [self.imageButtonThree setBackgroundImage:[UIImage imageWithData:dataPostOne] forState:UIControlStateNormal];
        self.deleteButtonThree.hidden = NO;
        [_imageArray addObject:dataPostOne];
        [_image_Array addObject:strOne];
    }
    NSLog(@" 不管了不想了不等了 %ld",(long)recordCount);
    
}

- (void)saveImage:(UIImage *)currentImage withName:(NSString *)imageName {
    
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 1.f);
    // 获取沙盒目录
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
    // 将图片写入文件
    [imageData writeToFile:fullPath atomically:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSData *)resetSizeOfImageData:(UIImage *)source_image maxSize:(NSInteger)maxSize{
    //先调整分辨率
    CGSize newSize = CGSizeMake(source_image.size.width, source_image.size.height);
    
    CGFloat tempHeight = newSize.height / 1024;
    CGFloat tempWidth = newSize.width / 1024;
    
    if (tempWidth > 1.0 && tempWidth > tempHeight) {
        newSize = CGSizeMake(source_image.size.width / tempWidth, source_image.size.height / tempWidth);
    } else if (tempHeight > 1.0 && tempWidth < tempHeight){
        newSize = CGSizeMake(source_image.size.width / tempHeight, source_image.size.height / tempHeight);
    }
    UIGraphicsBeginImageContext(newSize);
    [source_image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    //调整大小
    NSData *imageData = UIImageJPEGRepresentation(newImage, 1.0);
    
    NSUInteger sizeOrigin = [imageData length];
    NSUInteger sizeOriginKB = sizeOrigin / 1024;
    if (sizeOriginKB > maxSize) {
        NSData *finallImageData = UIImageJPEGRepresentation(newImage,0.3);
        return finallImageData;
    }
    return imageData;
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
